package com.prb;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;

public class NioServer {

    public static void main(String[] args) throws IOException {

        Selector selector = Selector.open();

        ServerSocketChannel ssc = ServerSocketChannel.open();

        ssc.bind(new InetSocketAddress(8080));
        System.out.println("web start");

        ssc.configureBlocking(false);

        ssc.register(selector , SelectionKey.OP_ACCEPT);

        while (true){
            int select = selector.select(100);
            Set<SelectionKey> selectionKeys = selector.selectedKeys();


        }




    }

    private static void handlerAccept(SocketChannel accept) {


        try {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            accept.read(byteBuffer);
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()) {
                char c = (char) byteBuffer.get();
                System.out.print("receiv:" + c);
            }
            // 此处打印执行的线程名称，永远为 main 线程
            System.out.println(Thread.currentThread().getName() + "开始向web返回消息。。。");
            ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
            // 给客户端一个响应，即向输出流写入信息
            String reply = "HTTP/1.1\n"; // 必须添加的响应头

            reply += "Content-type:text/html\n\n"; // 必须添加的响应头
            reply += "服务器返回的消息";
            byteBuffer2.put(new String(reply).getBytes());
            byteBuffer2.flip();
            System.out.println("ts");
            accept.write(byteBuffer2);
            accept.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
